// @description: 爬取数据
// @auth: bigkai
// @time: 2020/7/25

package crawl

import (
	"data"
	"fmt"
	"github.com/PuerkitoBio/goquery"
	"net/http"
	"strconv"
	"strings"
)

const BASE_URL = "http://fund.eastmoney.com/"

func CrwalData(numbers, errCode []int)  {

	go InitProxyPoll(20)
	proxyPool := GetProxyPool()

	for _, number := range numbers {
		var tturl = BASE_URL + strconv.Itoa(number) + ".html"

		resp, err := http.Get(tturl)
		if err != nil {
			panic(err)
		}
		defer resp.Body.Close()

		for {
			if resp == nil || data.IsContainer(errCode, resp.StatusCode) != -1 {
				resp = requestByProxy(tturl, <-proxyPool)
			}
		}


		document, err := goquery.NewDocumentFromReader(resp.Body)
		fund := Fund{}

		// 获取基金的净值估算、单位净值、累计净值
		document.Find("dd.dataNums").Each(func(idx int, element *goquery.Selection) {
			switch idx {
			case 0:
				fund.Jzgs = element.Find("span#gz_gsz").First().Text()
				fund.GsIncrease = element.Find("span#gz_gszzl").First().Text()
			case 1:
				fund.Dwjz = element.Find("span").First().Text()
				fund.DwIncrease = element.Find("span").Last().Text()
			case 2:
				fund.ljjz = element.Find("span").First().Text()
			}
		})

		// 获取基金的股票/债券持仓
		fund.SharesPosition = getPosition(true, document)
		fund.BondPosition = getPosition(false, document)


		fmt.Println(fund)
	}
}

// 获取股票(flag=true)/债券(flag=false)信息
func getPosition(flag bool, document *goquery.Document) ([]Position) {

	var name = ""
	if flag {
		name = "position_shares"
	} else {
		name = "position_bonds"
	}

	positions := []Position{}

	document.Find("li#" + name).Find(".poptableWrap").Find("table tbody tr").
		Each(func(i int, element *goquery.Selection) {
		if i != 0 {
			position := Position{}

			position.Name = strings.TrimSpace(element.Find(".alignLeft").First().Text())
			position.Proportion = strings.TrimSpace(element.Find(".alignRight").First().Text())
			position.Increase = strings.TrimSpace(element.Find(".alignRight").Last().Text())

			positions = append(positions, position)
		}
	})


	return positions
}
